package com.yaodian100.mall.channel.web;

import java.io.IOException;
import java.math.BigDecimal;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang.StringUtils;
import org.apache.struts2.ServletActionContext;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import com.yaodian100.core.common.dao.impl.CommonCriteria;
import com.yaodian100.core.common.service.DomainService;
import com.yaodian100.core.menu.entity.Menu;
import com.yaodian100.core.menu.entity.Option;
import com.yaodian100.core.template.service.TemplateService;
import com.yaodian100.ec.channel.entity.ChannelTemplate;
import com.yaodian100.ec.channel.service.ChannelService;
import com.yaodian100.ec.order.entity.Orders;
import com.yaodian100.ec.order.service.OrderService;

/**
 * 
 * @author apple.chang
 * 
 */
public class ReportCPSAction extends ActionSupport {

	private ChannelService channelService;
	private OrderService orderService;
	private TemplateService velocityService;
	private DomainService<Menu> menuService;
	private String date;
	private String site;

	/**
	 * 
	 */
	private static final long serialVersionUID = 8761064835879574234L;

	public String listByDay() throws IOException, ParseException {
		if (StringUtils.isBlank(date) || StringUtils.isBlank(site)) {
			return null;
		}
		HttpServletResponse response = (HttpServletResponse) ActionContext.getContext().get(
				ServletActionContext.HTTP_RESPONSE);

		ChannelTemplate ct = channelService.getAvailableChannelTemplate(site, "REPORT_CPS");
		if (null != ct) {
			CommonCriteria criteria = new CommonCriteria();
			DateFormat df = new SimpleDateFormat("yyyyMMddHHmmss");
			criteria.addGe("createDate", df.parse(date + "000000"));
			criteria.addLe("createDate", df.parse(date + "235959"));
			criteria.addEq("channel", site);
			List<Orders> orderList = orderService.getList(0, -1, criteria, new String[] { "createDate asc" });
			Map<String, BigDecimal> priceMap = new HashMap<String, BigDecimal>();
			if ("fanli".equals(site)) {
				countFanliRate(orderList, priceMap);
			}
			Map<String, Object> m = new HashMap<String, Object>();
			m.put("orderList", orderList);
			m.put("channelService", channelService);
			m.put("orderService", orderService);
			m.put("priceMap", priceMap);
			response.setContentType("text/html;charset=UTF-8");
			response.setCharacterEncoding("UTF-8");
			response.getWriter().print(velocityService.formatByStringTemplate(ct.getContents(), m));
		}
		return null;
	}

	private void countFanliRate(List<Orders> orderList, Map<String, BigDecimal> priceMap) {
		Menu rateMenu = menuService.get("Fanli.Rate");
		Map<String, Option> rateOption = rateMenu.getOptions();
		for (int i = 0; i < orderList.size(); i++) {
			Orders orders = orderList.get(i);
			List<String> odList = orderService.getChannelList(orders.getOrderId());
			Object[] obj = odList.toArray();
			boolean useMemo2 = false;
			if (orders.getBonus().floatValue() > 0) {
				useMemo2 = true;
			}
			for (int iOd = 0; iOd < obj.length; iOd++) {
				Object[] obj2 = (Object[]) obj[iOd];
				String rate = "0";
				Option o = rateOption.get(obj2[3].toString().split("/")[0]);
				if (null != o) {
					if (useMemo2 && StringUtils.isNotBlank(o.getMemo2())) {
						rate = o.getMemo2();
					} else {
						if (!obj2[5].toString().equals(obj2[6].toString())) {
							rate = o.getMemo2();
						} else {
							rate = o.getMemo1();
						}
					}
				}
				priceMap.put(orders.getOrderId() + "_" + obj2[0].toString(), new BigDecimal(obj2[2].toString()).multiply(
						new BigDecimal(rate)).setScale(2, BigDecimal.ROUND_HALF_UP));
			}
		}
	}

	public ChannelService getChannelService() {
		return channelService;
	}

	public void setChannelService(ChannelService channelService) {
		this.channelService = channelService;
	}

	public String getDate() {
		return date;
	}

	public void setDate(String date) {
		this.date = date;
	}

	public OrderService getOrderService() {
		return orderService;
	}

	public void setOrderService(OrderService orderService) {
		this.orderService = orderService;
	}

	public TemplateService getVelocityService() {
		return velocityService;
	}

	public void setVelocityService(TemplateService velocityService) {
		this.velocityService = velocityService;
	}

	public String getSite() {
		return site;
	}

	public void setSite(String site) {
		this.site = site;
	}

	public DomainService<Menu> getMenuService() {
		return menuService;
	}

	public void setMenuService(DomainService<Menu> menuService) {
		this.menuService = menuService;
	}
}
